package com.gxuwz.zlc.controller;

import com.gxuwz.zlc.common.Result;
import com.gxuwz.zlc.entity.Repair;
import com.gxuwz.zlc.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 维修
 */
@Controller
@RequestMapping("/repair")
public class RepairController {

    @Autowired
    private RepairService repairService;

    // 查询所有
    @RequestMapping("/findAll")
    @ResponseBody
    public List<Repair> findRepair(){
        List<Repair> repairList = repairService.findRepair();
        return repairList;
    }

    // 添加
    @RequestMapping("/add")
    @ResponseBody
    public Map addRepair(@RequestBody Repair repair){
        repair.setRepairState("0"); // 设置状态 0未维修 1维修
        repairService.addRepair(repair);
        Map<String,String> map = new HashMap<>();
        map.put("code","200");
        map.put("msg","修改成功");
        return map;
    }


    // 修改
    @RequestMapping("/update")
    @ResponseBody
    public Map updateRepair(@RequestBody Repair repair){
        repairService.updateRepair(repair);
        Map<String,String> map = new HashMap<>();
        map.put("code","200");
        map.put("msg","修改成功");
        return map;
    }

    // 删除
    @RequestMapping("/delete")
    @ResponseBody
    public Map deleteRepair(@RequestParam(name = "repairId") Integer repairId){
       repairService.deleteRepair(repairId);
        Map<String,String> map = new HashMap<>();
        map.put("code","200");
        map.put("mag","删除成功");
        return map;
    }

    /**
     * 查询一个
     * @param repairId
     * @return
     */
    @GetMapping("/findOne/{repairId}")
    @ResponseBody
    public Result get(@PathVariable Integer repairId){
        Repair repairById = repairService.findRepairById(repairId);
        // code=0,msg=成功
        return Result.success(repairById);
    }

    @RequestMapping("/print")
    public String print(@RequestParam Integer repairId, Model model){
        Repair repairById = repairService.findRepairById(repairId);
        model.addAttribute("repairById",repairById);
        return "technical/print_repair";
    }


}
